<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          class="handle-del mr10"
          @click="openAddDialog"
          size="small"
          >新增折扣</el-button
        ><br /><br />
        <el-form label-width="70px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="会员类型">
                <el-select v-model="query.vipType_id" placeholder="请选择">
                  <el-option
                    v-for="item in vipTypeArr"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button type="primary" @click="clearSearch" size="small"
                >清空</el-button
              >
              <el-button type="primary" @click="handleSearch" size="small"
                >搜索</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>

      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">{{
            (query.pageNum - 1) * query.pageSize + (scope.$index + 1)
          }}</template>
        </el-table-column>
        <el-table-column
          prop="type"
          label="会员类型"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="waresType"
          label="商品类型"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="discount"
          label="折扣"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="birthdayDiscount"
          label="生日折扣"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="materialDiscount"
          label="料价优惠"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="manual"
          label="工费优惠"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <el-dialog
      :title="dialogTitle"
      :visible.sync="editVisible"
      width="70%"
      :close-on-click-modal="false"
    >
      <el-form ref="form" border :model="addForm" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="会员类型">
              <el-select v-model="addForm.vipType_id" placeholder="请选择">
                <el-option
                  v-for="item in vipTypeArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品类型">
              <el-input
                placeholder="请输入名称"
                v-model="addForm.waresType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="折扣">
              <el-input
                placeholder="请输入折扣"
                v-model="addForm.discount"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="生日折扣">
              <el-input
                placeholder="请输入生日折扣"
                v-model="addForm.birthdayDiscount"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="料价优惠">
              <el-input
                placeholder="请输入料价优惠"
                v-model="addForm.materialDiscount"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工费优惠">
              <el-input
                placeholder="请输入工费优惠"
                v-model="addForm.manual"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="addUpdCusInfo" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <template> </template>
  </div>
</template>

<script>
let queryParam = { vipType_id: "", pageNum: 1, pageSize: 5 };
let addFormParam = {
  id: "",
  vipType: "1",
  vipType_id: "1",
  waresType: "",
  discount: "",
  birthdayDiscount: "",
  materialDiscount: "",
  manual: ""
};

export default {
  data() {
    return {
      query: { ...queryParam },
      tableData: [],
      multipleSelection: [],
      delList: [],
      dialogTitle: "",
      editVisible: false,
      pageTotal: 0,
      addForm: { ...addFormParam },
      allRoleName: [],
      idx: -1,
      id: -1,
      isAdd: false,
      vipTypeArr: [
        { value: "1", label: "普通会员" },
        { value: "2", label: "银卡会员" },
        { value: "3", label: "金卡会员" },
        { value: "4", label: "白金会员" }
      ]
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$axios
        .post(this.$api.discount.findAll, this.query)
        .then(res => {
          if (res.data.code == "1" && res.data.success == true) {
            this.tableData = res.data.data.list;
            this.pageTotal = res.data.data.total;
          }
        })
        .catch(err => {
          console.log(err);
          this.tableData = [];
          this.pageTotal = 0;
        });
    },
    //新增或修改用户信息
    addUpdCusInfo() {
      if (this.isAdd) {
        this.addForm.vipType = this.addForm.vipType_id;
        this.$axios
          .post(this.$api.discount.insert, this.addForm)
          .then(res => {
            if (res.data.code == "1" && res.data.success == true) {
              this.$message.success("新增成功");
            }
            this.getData();
            this.editVisible = false;
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        this.$axios
          .post(this.$api.discount.update, this.addForm)
          .then(res => {
            //console.log("修改的结果为："+JSON.stringify(res));
            if (res.data.code == "1" && res.data.success == true) {
              this.$message.success("修改成功");
            }
            this.getData();
            this.editVisible = false;
          })
          .catch(err => {
            console.log(err);
          });
        this.$set(this.tableData, this.idx, this.form);
      }
    },
    //清空搜索条件
    clearSearch() {
      this.query = { ...queryParam };
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageNum", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.$axios
            .get(this.$api.discount.delete + "?id=" + row.id)
            .then(res => {
              if (res.data.code == "1" && res.data.success == true) {
                this.$message.success("删除成功");
                this.getData();
              } else if (res.data.code == "0" && res.data.success == false) {
                this.$message.warning(res.data.msg);
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //打开新增员工的弹框
    openAddDialog() {
      this.addForm = { ...addFormParam };
      this.dialogTitle = "新增折扣信息";
      this.isAdd = true;
      this.editVisible = true;
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.addForm = row;
      this.addForm.vipType_id = row.vipType_id + "";
      this.dialogTitle = "编辑折扣信息";
      this.isAdd = false;
      this.editVisible = true;
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageNum", val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
